# windows下进行发行版编译
# cmake -DCMAKE_BUILD_TYPE=Release -GNinja ..
# linux下使用musl库编译
# cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=/opt/musl/bin/g++ -DCMAKE_C_COMPILER=/opt/musl/bin/gcc -GNinja ..
# cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc -GNinja ..
# 生成asm信息（带源文件） gcc -S -fverbose-asm -masm=intel $*

# cmake需要的版本号
cmake_minimum_required(VERSION 3.10)

# 模块目录
#set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/libressl;${CMAKE_MODULE_PATH}")
#set(LIBRESSL_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libressl")

# 项目设置
project(http_server)

#find_package(LibreSSL REQUIRED)

# 设置c和c++标准版本
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_COLOR_MAKEFILE OFF)

# 全局宏定义  -D_POSIX_C_SOURCE -DFMT_HEADER_ONLY -DNLOG
# add_definitions(-DMG_ENABLE_LOG=0)

# 设置c和c++编译选项 -fno-rtti -fno-exceptions -fsanitize=leak
set(CMAKE_C_FLAGS "-Wall -Wno-unknown-pragmas -fPIC")
set(CMAKE_CXX_FLAGS "-Wall -Wno-unknown-pragmas -fPIC --coroutines")
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    set(CMAKE_C_FLAGS_DEBUG "-fsanitize=address")
    set(CMAKE_CXX_FLAGS_DEBUG "-fsanitize=address")
endif ()
set(CMAKE_C_FLAGS_RELEASE "-O2 -flto -ffunction-sections -fdata-sections")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -flto -Wno-mismatched-new-delete -Wno-odr -ffunction-sections -fdata-sections")

# 如果找到ccache，设置使用ccache作为编译缓存工具
# find_program(CCACHE_FOUND ccache)
# if(CCACHE_FOUND)
#     set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
#     set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
# endif(CCACHE_FOUND)

#set(CMAKE_C_COMPILER gcc)
#set(CMAKE_CXX_COMPILER g++)

# 设置include和lib的目录
include_directories("third_party")
# include_directories("libressl/include")
# link_directories(AFTER "libressl/build/crypto" "libressl/build/ssl" "libressl/build/tls")

# windows默认debug模式，linux默认release模式
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
    set(OSN "win")
    if (NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Debug")
    endif ()
    add_definitions(-D_WIN64 -D_WIN32_WINNT=0x0601)
    # add_definitions(-DSPDLOG_COMPILED_LIB -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE -DSPDLOG_FMT_EXTERNAL)
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
    set(OSN "linux")
    if (NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE "Release")
    endif ()
    # add_definitions(-DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE -DSPDLOG_FMT_EXTERNAL -DFMT_HEADER_ONLY)
    # add_definitions(-DSPDLOG_NO_EXCEPTIONS -DSPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_TRACE -DSPDLOG_FMT_EXTERNAL -DFMT_HEADER_ONLY -DFMT_USE_NOEXCEPT)
else ()
    message(SEND_ERROR, "unsupport platform ${CMAKE_SYSTEM_NAME}")
endif ()

# target_link_libraries(${PROJECT_NAME} PUBLIC mongoose_${OSN}_x64)

# 针对调试版本和发行版本设置编译标志和链接标志
if (CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Release"))
    add_definitions(-DNDEBUG)
    add_link_options(-static -s -Wl,-gc-sections)
    # add_link_options(-s -Wl,-gc-sections)
else ()
    # add_definitions(-DDEBUG)
    # target_link_libraries(${PROJECT_NAME} PUBLIC fmt_${OSN}_x64 spdlog_${OSN}_x64)
endif ()

if (CMAKE_SYSTEM_NAME MATCHES "Windows")
    link_libraries(ws2_32 mswsock)
    add_link_options(-static)
elseif (CMAKE_SYSTEM_NAME MATCHES "Linux")
    link_libraries(pthread)
endif()

# file(GLOB_RECURSE UUID_SRCS
# 	${CMAKE_CURRENT_SOURCE_DIR}/third_party/uuid/*.c
# )

# aux_source_directory(library/klib KLIB_SRCS)
file(GLOB_RECURSE KLIB_SRCS
	${CMAKE_CURRENT_SOURCE_DIR}/third_party/klib/*.c
	${CMAKE_CURRENT_SOURCE_DIR}/third_party/klib/*.cpp
)

file(GLOB_RECURSE KLIB_HTTP_SRCS
	${CMAKE_CURRENT_SOURCE_DIR}/third_party/klib_http/*.c
	${CMAKE_CURRENT_SOURCE_DIR}/third_party/klib_http/*.cpp
	${CMAKE_CURRENT_SOURCE_DIR}/third_party/llhttp/*.c
	${CMAKE_CURRENT_SOURCE_DIR}/third_party/yyjson/*.c
)

# aux_source_directory(src/common COMMON_SRCS)
# file(GLOB_RECURSE EXT_SRCS
# 	${CMAKE_CURRENT_SOURCE_DIR}/ext/*.c
# 	${CMAKE_CURRENT_SOURCE_DIR}/ext/*.cpp
# )

# 库编译 STATIC/SHARED/OBJECT
# add_library(klib STATIC ${KLIB_SRCS})
add_library(klib STATIC ${KLIB_SRCS} ${KLIB_HTTP_SRCS})

# if(CMAKE_SYSTEM_NAME MATCHES "Linux")
#     add_library(uuid STATIC ${UUID_SRCS})
# endif ()

# 可执行程序
add_executable(accinfo
    src/accinfo.cpp
    src/aidb.cpp
    src/api.cpp
    src/app.cpp
    src/dbcache.cpp
)

target_link_libraries(accinfo klib)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    target_link_libraries(accinfo uuid)
endif ()

add_executable(test_dbm
    src/test/test_dbm.cpp
)
target_link_libraries(test_dbm klib)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    target_link_libraries(test_dbm uuid)
endif ()

add_executable(test_aidb
    src/test/test_aidb.cpp
    src/aidb.cpp
)
target_link_libraries(test_aidb klib)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    target_link_libraries(test_aidb uuid)
endif ()

add_executable(test_url
    src/test/test_url.cpp
)
target_link_libraries(test_url klib)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    target_link_libraries(test_url uuid)
endif ()

add_executable(test_url_enc_dec
    src/test/test_url_enc_dec.cpp
)
target_link_libraries(test_url_enc_dec klib)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    target_link_libraries(test_url_enc_dec uuid)
endif ()

add_executable(test
    src/test/test.cpp
)
target_link_libraries(test klib)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
    target_link_libraries(test uuid)
endif ()

add_executable(test_async
    src/test/test_async.cpp
    third_party/klib/printf.cpp
    third_party/klib/format.cpp
    third_party/klib/datetime.cpp
    third_party/klib/log.cpp
)

add_executable(test_printf
    src/test/test_printf.cpp
    third_party/klib/printf.cpp
)

add_executable(test_format
    src/test/test_format.cpp
    third_party/klib/datetime.cpp
    third_party/klib/format.cpp
)

add_executable(test_utils
    src/test/test_utils.cpp
    third_party/klib/printf.cpp
    third_party/klib/format.cpp
    third_party/klib/datetime.cpp
)

add_executable(test_objpool
    src/test/test_objpool.cpp
)


# set(CMAKE_VERBOSE_MAKEFILE ON)
# 或者用 make VERBOSE=1 来显示编译详情

# libressl配置
# target_link_libraries(test_quick crypto-46 ssl-48 tls-20)
